<?php
class News extends CI_Controller {

  public function __construct()
  {
    parent::__construct();
    $this->load->model('news_model');
  }

  public function index($p=0)
  {
	      //不分页
      $data['news'] = $this->news_model->get_news();
	  $data['title'] = '这是新闻读取首页';
	
	  $this->load->view('templates/header', $data);
	  $this->load->view('news/index', $data);
	  $this->load->view('templates/footer');
	   
	  //分页
//	  $this->load->library('pagination');
//
//		$config['base_url'] = 'http://ci.me/news/page/';
//		$config['total_rows'] = 200;
//		$config['per_page'] = 20; 
//
//		$this->pagination->initialize($config); 
//		echo $this->pagination->create_links();page_news

  }
  public function page($p=0){
	  $n = $this->news_model->get_news ();
//		print_r( ($n) );
//		echo json_encode($n);
//		die();
//		

//		$this->config->load ( 'pagination' );
		
		$this->load->library ( 'pagination' );
		//print_r($this->config->item('per_page') );
		

		$config ['base_url'] = '/news/page';
		
		$config ['total_rows'] = count($n);// $n->num_rows ();
		$config ['per_page'] = 2;
		$config ['full_tag_open'] = '<p>';
		$config ['full_tag_close'] = '</p>';
		
		$config ['first_link'] = 'First';
		$config ['first_tag_open'] = '<div>';
		$config ['first_tag_close'] = '</div>';
		$config ['last_link'] = 'Last';
		$config ['last_tag_open'] = '<div>';
		$config ['last_tag_close'] = '</div>';
		$this->pagination->initialize ( $config );
		
		$data ['title'] = '分页test';
		$data ['news'] = $this->news_model->page_news ( $p,2 );
		$data ['pageinfo'] = $this->pagination->create_links ();
		$this->load->view ( 'templates/header', $data );
		$this->load->view ( 'news/page', $data );
		$this->load->view ( 'templates/footer' );
		 
  }

  public function view($slug)
  {
//  	$this->output->enable_profiler(TRUE);
  	$data['news_item'] = $this->news_model->get_news($slug);
  	if(empty($data['news_item'])){
  		show_404();
  	}
  	$data['title'] = $data['news_item']['title'];
  	$this->load->view('templates/header', $data);
  	$this->load->view('news/view', $data);
  	$this->load->view('templates/footer');
//  	$this->output->cache(1);
  }
  
  public function create()
  {
  	$this->load->helper('form');
  	$this->load->library('form_validation');

  	$data['title'] = 'Create a news item';

  	$this->form_validation->set_rules('title', 'Title', 'required');
  	$this->form_validation->set_rules('text', 'text', 'required');

  	if ($this->form_validation->run() === FALSE)
  	{
  		$this->load->view('templates/header', $data);
  		$this->load->view('news/create');
  		$this->load->view('templates/footer');

  	}
  	else
  	{
  		$this->news_model->set_news();
  		$this->load->view('news/success');
  	}
  }
}
?>